package org.zty.util;

import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;


public class RequestUtil {
	/**
	 * 通过HttpServletRequest获取对应的参数值
	 * @param request
	 * @param param
	 * @return 如果值为Null时返回“”，否则返回对应值
	 */
	public static String getParameter(HttpServletRequest request,String param)
	{
		return request.getParameter(param)==null?"":request.getParameter(param).trim();
	}
	/**
	 * 获取request中的字符
	 * @param request
	 * @param name
	 * @return
	 */
	public static String toStr(HttpServletRequest request,String name){
		return request.getParameter(name)==null?"":request.getParameter(name).trim();
	}
	public static String toStr(HttpServletRequest request,String name,String defaultValue){
		return request.getParameter(name)==null?defaultValue:request.getParameter(name).trim();
	}
	
	
	public static String[] toStrs(HttpServletRequest request,String name){
		try {
			String[] s = request.getParameterValues(name);
			return s;
		} catch (Exception e) {
			return new String[]{};
		}
	}
	
	public static String[] toStrs(HttpServletRequest request,String name,String[] defaultValue){
		try {
			String[] s = request.getParameterValues(name);
			return s;
		} catch (Exception e) {
			return defaultValue;
		}
	}
	
	
	public static int toInt(HttpServletRequest request,String name)
	{
		try {
			String s = request.getParameter(name).trim();
			return Integer.parseInt(s);
		} catch (Exception e) {
			return 0;
		}
	}
	public static int toInt(HttpServletRequest request,String name,int defaultValue){
		try {
			String s = request.getParameter(name).trim();
			return Integer.parseInt(s);
		} catch (Exception e) {
			return defaultValue;
		}
	}
	
	public static int[] toInts(HttpServletRequest request,String name){
		try {
			String[] s = request.getParameterValues(name);
			int[] returnValue= new int[s.length];
			for(int i =0;i<s.length;i++){
				returnValue[i] = Integer.parseInt(s[i].trim());
			}
			return returnValue;
		} catch (Exception e) {
			return new int[]{};
		}
	}
	
	public static int[] toInts(HttpServletRequest request,String name,int[] defaultValue){
		try {
			String[] s = request.getParameterValues(name);
			int[] returnValue= new int[s.length];
			for(int i =0;i<s.length;i++){
				returnValue[i] = Integer.parseInt(s[i].trim());
			}
			return returnValue;
		} catch (Exception e) {
			return defaultValue;
		}
	}
	
	public static boolean[] toBoolean(HttpServletRequest request,String name){
		try {
			String[] s = request.getParameterValues(name);
			boolean[] returnValue= new boolean[s.length];
			for(int i =0;i<s.length;i++){
				returnValue[i] = Boolean.parseBoolean(s[i].trim());
			}
			return returnValue;
		} catch (Exception e) {
			return new boolean[]{};
		}
	}
	
	public static boolean[] toBoolean(HttpServletRequest request,String name,boolean[] defaultValue){
		try {
			String[] s = request.getParameterValues(name);
			boolean[] returnValue= new boolean[s.length];
			for(int i =0;i<s.length;i++){
				returnValue[i] = Boolean.parseBoolean(s[i].trim());
			}
			return returnValue;
		} catch (Exception e) {
			return defaultValue;
		}
	}
	
	public static long toLong(HttpServletRequest request,String name)
	{
		try {
			String s = request.getParameter(name).trim();
			return Long.parseLong(s);
		} catch (Exception e) {
			return 0l;
		}
	}
	public static long toLong(HttpServletRequest request,String name,long defaultValue)
	{
		try {
			String s = request.getParameter(name).trim();
			return Long.parseLong(s);
		} catch (Exception e) {
			return defaultValue;
		}
	}
	public static Date toDate(HttpServletRequest request,String name,String parsePatterns)throws ZException{
		try {
			String t = request.getParameter(name)==null?"":request.getParameter(name).trim();
			return DateUtil.parseDate(t, parsePatterns);
		} catch (Exception e) {
			throw new ZException("转换日期失败",e);
		}
	}
	public static Date toDate(HttpServletRequest request,String name,String parsePatterns,Date defaultValue)throws ZException{
		try {
			String t = request.getParameter(name)==null?"":request.getParameter(name).trim();
			if("".equals(t)){
				return defaultValue;
			}else{
				return DateUtil.parseDate(t, parsePatterns);
			}
		} catch (Exception e) {
			throw new ZException("转换日期失败",e);
		}
	}
	public static float toFloat(HttpServletRequest request,String name)
	{
		try {
			String s = request.getParameter(name).trim();
			return Float.parseFloat(s);
		} catch (Exception e) {
			return 0.0f;
		}
	}
	public static double toDouble(HttpServletRequest request,String name)
	{
		try {
			String s = request.getParameter(name).trim();
			return Double.parseDouble(s);
		} catch (Exception e) {
			return 0.0d;
		}
	}
	public static boolean toBool(HttpServletRequest request,String name,boolean def){
		try {
			String s = toStr(request, name);
			return Boolean.parseBoolean(s);
		} catch (Exception e) {
			return def;
		}
	}
	public static String getPageParamenters(HttpServletRequest req,boolean flag){
		StringBuffer par=new StringBuffer();
			Enumeration paraname = req.getParameterNames();
			String tmp="";
			while(paraname.hasMoreElements())
			{
				tmp =paraname.nextElement().toString();
//				par = par+"&"+tmp+"="+req.getParameter(tmp);
				if(flag){
					par.append("?");
					flag = false;
				}
				else{
				par.append("&");
				}
				par.append(tmp);
				par.append("=");
				par.append(req.getParameter(tmp));
			}
			return par.toString();
	}
	public static boolean checkServletPath(HttpServletRequest request,String path){
		if(request.getServletPath().indexOf(path)!=-1){
			return true;
		}else
			return false;
	}
	public static String outSpecificStr(HttpServletRequest request,String path,String out1,String out2){
		if(checkServletPath(request, path)){
			return out1;
		}else{
			return out2;
		}
	}
	public static String outSpecificStr(HttpServletRequest request,String path,String out){
		return outSpecificStr(request,path,out,"");
	}
}
